package com.iseastar.guojiang.newcabinet;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.app.ocr.ui.camera.CameraNativeHelper;
import com.app.ocr.ui.camera.CameraView;
import com.app.ocr.ui.camera.OCRCameraActivity;
import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.AccessToken;
import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.BeeApplication;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.identity.SenderAuthenticationActivity;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.NewParcelBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.newcabinet.adapter.ParcelListAdapter;
import com.shouhuobao.station.R;
import com.shouhuobao.station.wxapi.Constants;
import com.shouhuobao.take.utils.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import droid.frame.utils.android.Log;
import droid.frame.utils.lang.FileUtils;
import droid.frame.utils.lang.Str;

/**
 * 包裹列表
 * Created by lidandan on 2017/12/20.
 */
public class ParcelListActivity extends BaseActivity2 implements View.OnClickListener {
    private static final int REQUEST_CODE_CAMERA = 102;

    private ListView mListView;
    private ParcelListAdapter mAdapter;
    //包裹列表数据
    private ArrayList<NewParcelBean> items;
    //保存上传后的图片地址,给服务端进行上传
    private String photoUrl = "";
    private int position = 0;
    private NewParcelBean parcelBean = null;
    private String idCardName = "";
    private String idCardNumber = "";
    //袋子编号
    private String bagCode = "";
    private boolean hasGotToken = false;
    private TextView mWithOrWithoutRealTV;
    private TextView mAtOnceRealTV;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        items = (ArrayList<NewParcelBean>) getIntent().getSerializableExtra("items");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_parcel_list);
        super.findViewById();
        getAppTitle().setCommonTitle("包裹列表");
        //有无实名
        mWithOrWithoutRealTV = (TextView) findViewById(R.id.with_or_without_real_tv);
        mAtOnceRealTV = (TextView) findViewById(R.id.at_once_real_tv);
        mAtOnceRealTV.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.listview);
        findViewById(R.id.finish_bt).setOnClickListener(this);
        mAdapter = new ParcelListAdapter(items, getContext(), mListView, handler);
        mListView.setAdapter(mAdapter);
        if (items != null && items.get(0) != null) {
            if (items.get(0).getTaskType() == Const.CourierTaskStatus.visitTask) {
                //审核中或审核成功
                if (items.get(0).isRealNameStatus()) {
                    findViewById(R.id.id_card_layout).setVisibility(View.GONE);
                } else {
                    findViewById(R.id.id_card_layout).setVisibility(View.VISIBLE);
                    initAccessTokenWithAkSk();
                    mWithOrWithoutRealTV.setText("该用户（" + items.get(0).getUserPhone() + "）尚未实名，请按照相关规定对用户进行实名认证");
                }
            } else {
                findViewById(R.id.id_card_layout).setVisibility(View.GONE);
            }
        } else {
            findViewById(R.id.id_card_layout).setVisibility(View.GONE);
        }
    }

    //用于处理适配器的操作
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            parcelBean = (NewParcelBean) msg.obj;
            position = msg.arg1;//要操作的item
            int currentPosition = msg.arg2;
            /**
             * 0 删除拍照后的图片 1 拒收  2 对包裹进行拍照  3 扫描袋子进行绑定 4 重新获开箱码 5 点击图片查看包裹拍照后的大图片
             */
            Intent intent = null;
            if (parcelBean == null) {
                return;
            }
            switch (what) {
                //删除拍照后的图片
                case 0:
                    showLoadingDialog(null);
                    AppHttp.getInstance().courierParcelImageData(parcelBean.getDetailId(), 1, parcelBean.getDeleteParcelImage(), position);
                    break;
                //拒收包裹
                case 1:
                    intent = new Intent(getContext(), ParcelRejectActivity.class);
                    intent.putExtra("item", parcelBean);
                    intent.putExtra("currentPosition", position);
                    startActivityForResult(intent, 1008);
                    break;
                //对包裹进行拍照
                case 2:
                    intent = new Intent(getContext(), CourierCameraOneToOneActivity.class);
                    startActivityForResult(intent, 1005);
                    break;
                //扫描袋子编号
                case 3:
                    intent = new Intent();
                    intent.setClass(getContext(), CourierCaptureActivity.class);
                    intent.setAction(Intents.courier_scan_bind_bagcode);
                    startActivityForResult(intent, 101);
                    break;
                //重新获取开箱码
                case 4:
                    showLoadingDialog(null);
                    AppHttp.getInstance().reGetOpenBoxCode(parcelBean.getParcelId(), position);
                    break;
                //点击图片查看包裹拍照后的大图片
                case 5:
                    intent = new Intent(getContext(), ParcelImageLargeActivity.class);
                    intent.putExtra("item", parcelBean);
                    intent.putExtra("currentPosition", position);
                    intent.putExtra("imageCurrentPosition", currentPosition);
                    startActivityForResult(intent, 1007);
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    };

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //上传头像返回图片路径
            case MsgID.upload_image:
                ReqResult<?> reqResult = JSON.parser(msg.obj);
                if (checkLoginStatus(reqResult)) {
                    Object imageUrl = reqResult.getResult();
                    if (!isEmpty(imageUrl)) {
                        photoUrl = imageUrl + "";
                        if (parcelBean != null) {
                            AppHttp.getInstance().courierParcelImageData(parcelBean.getDetailId(), 2, photoUrl, position);
                        } else {
                            cancelLoadingDialog();
                            showToast("上传图片失败");
                        }
                    } else {
                        photoUrl = "";
                        cancelLoadingDialog();
                        showToast("上传图片失败");
                    }
                } else {
                    cancelLoadingDialog();
                    photoUrl = "";
                    showToast(reqResult.getMessage());
                }
                return false;
            //上传用户实名认证图片
            case MsgID.courier_parcel_list_scan_userIdCard_image:
                ReqResult<String> idCardImageResult = JSON.parser(msg.obj, String.class);
                if (idCardImageResult.getResult() == null) {
                    cancelLoadingDialog();
                    showToast("图片上传失败");
                    return false;
                }
                if (checkLoginStatus(idCardImageResult)) {
                    String path = idCardImageResult.getResult().replace("\\", "").replace("[", "").replace("]", "").replace("\"", "");
                    if (isEmpty(path)) {
                        cancelLoadingDialog();
                        showToast("图片上传失败");
                        return true;
                    }
                    AppHttp.getInstance().courierParcelListUserIdCardSubmit(mAdapter.getItem(0).getOrderUserPhone(), idCardName, idCardNumber, path, "");
                } else {
                    cancelLoadingDialog();
                    showToast(idCardImageResult.getMessage());
                }
                return true;
            //完成揽收包裹返回
            case MsgID.courier_parcel_list_take_data:
                cancelLoadingDialog();
                ReqResult<?> finishResult = JSON.parser(msg.obj);
                if (checkLoginStatus(finishResult)) {
                    if ((Boolean) finishResult.getResult()) {
                        if (isGoToMainActivity()) {
                            goToMainActivity();
                        } else {
                            startActivity(TaskFinishActivity.class);
                            finish();
                        }
                    } else {
                        showToast("揽收失败");
                    }
                } else {
                    showToast(finishResult.getMessage());
                }
                return false;
            //同城完成揽收包裹返回
            case MsgID.courier_samecity_task_complete:
                cancelLoadingDialog();
                final ReqResult<ServiceTaskBean> sameCityResult = JSON.parser(msg.obj, ServiceTaskBean.class);
                if (checkLoginStatus(sameCityResult)) {
                    if (isGoToMainActivity()) {
                        goToMainActivity();
                    } else {
                        if (sameCityResult.getResult() != null) {
                            Intent intent = new Intent(getContext(), SameCityPickupCompleteActivity.class);
                            intent.putExtra("item", sameCityResult.getResult());
                            startActivity(intent);
                            finish();
                        } else {
                            showToast("揽收失败");
                        }
                    }
                } else {
                    showToast(sameCityResult.getMessage());
                }
                return false;
            //图片新增
            case MsgID.courier_parcel_process_image_data:
                cancelLoadingDialog();
                final int refreshPosition = msg.arg1;
                final ReqResult<?> addImageResult = JSON.parser(msg.obj);
                if (checkLoginStatus(addImageResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if ((Boolean) addImageResult.getResult()) {
                                NewParcelBean bean = mAdapter.getItem(refreshPosition);
                                if (isEmpty(bean.getBagImg1())) {
                                    bean.setBagImg1(photoUrl);
                                } else if (isEmpty(bean.getBagImg2())) {
                                    bean.setBagImg2(photoUrl);
                                } else if (isEmpty(bean.getBagImg3())) {
                                    bean.setBagImg3(photoUrl);
                                }
                                mAdapter.updateItemView(refreshPosition, bean);
                            } else {
                                showToast("拍照上传失败");
                            }
                        }
                    });
                } else {
                    showToast(addImageResult.getMessage());
                }
                return true;
            //图片删除
            case MsgID.courier_parcel_process_image_data + 1:
                cancelLoadingDialog();
                final ReqResult<?> deleteImageResult = JSON.parser(msg.obj);
                final int refreshPosition1 = msg.arg1;
                if (checkLoginStatus(deleteImageResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if ((Boolean) deleteImageResult.getResult()) {
                                NewParcelBean bean = mAdapter.getItem(refreshPosition1);
                                if (isNotEmpty(bean.getBagImg1()) && isNotEmpty(parcelBean.getDeleteParcelImage()) && bean.getBagImg1().equals(parcelBean.getDeleteParcelImage())) {
                                    bean.setBagImg1("");
                                } else if (isNotEmpty(bean.getBagImg2()) && isNotEmpty(parcelBean.getDeleteParcelImage()) && bean.getBagImg2().equals(parcelBean.getDeleteParcelImage())) {
                                    bean.setBagImg2("");
                                } else if (isNotEmpty(bean.getBagImg3()) && isNotEmpty(parcelBean.getDeleteParcelImage()) && bean.getBagImg3().equals(parcelBean.getDeleteParcelImage())) {
                                    bean.setBagImg3("");
                                }
                                bean.setDeleteParcelImage("");
                                mAdapter.updateItemView(refreshPosition1, bean);
                            } else {
                                showToast("拍照上传失败");
                            }
                        }
                    });
                } else {
                    showToast(deleteImageResult.getMessage());
                }
                return false;
            //拒绝揽收包裹
            case MsgID.courier_rejection_parcel_data:
                cancelLoadingDialog();
                final int rejectionPosition = msg.arg1;
                final ReqResult<?> rejectionResult = JSON.parser(msg.obj);
                if (checkLoginStatus(rejectionResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if ((Boolean) rejectionResult.getResult()) {
                                NewParcelBean bean = mAdapter.getItem(rejectionPosition);
                                bean.setRejectStatus(true);
                                mAdapter.updateItemView(rejectionPosition, bean);
                            } else {
                                showToast("拒收失败");
                            }
                        }
                    });
                } else {
                    showToast(rejectionResult.getMessage());
                }
                return true;
            //给包裹绑定袋子编号
            case MsgID.courier_bagcode_bindparcel:
                cancelLoadingDialog();
                final int bindParcelPosition = msg.arg1;
                ReqResult<?> bindBagResult = JSON.parser(msg.obj);
                if (checkLoginStatus(bindBagResult)) {
                    showToast("绑定成功");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            NewParcelBean parcelBean = mAdapter.getItem(bindParcelPosition);
                            parcelBean.setBagCode(bagCode);
                            bagCode = "";
                            mAdapter.updateItemView(bindParcelPosition, parcelBean);
                        }
                    });
                } else {
                    bagCode = "";
                    showToast(bindBagResult.getMessage());
                }
                return true;
            //获取开箱码
            case MsgID.courier_reget_open_box_code:
                cancelLoadingDialog();
                final int boxPosition = msg.arg1;
                final ReqResult<?> boxCodeResult = JSON.parserMap(msg.obj, Object.class);
                if (checkLoginStatus(boxCodeResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            HashMap<String, Object> map = (HashMap<String, Object>) boxCodeResult.getResultMap();
                            if (map != null && map.size() > 0) {
                                String openCode = (String) map.get("checkCode");
                                NewParcelBean parcelBean = mAdapter.getItem(boxPosition);
                                parcelBean.setPassword2(openCode);
                                mAdapter.updateItemView(boxPosition, parcelBean);
                            } else {
                                showToast("获取开箱码失败");
                            }
                        }
                    });
                } else {
                    showToast(boxCodeResult.getMessage());
                }
                return true;
            //提交用户实名认证返回
            case MsgID.courier_parcel_list_userIdCard_submit:
                cancelLoadingDialog();
                ReqResult<?> idCardResult = JSON.parser(msg.obj);
                if (checkLoginStatus(idCardResult)) {
                    showToast("提交成功");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAtOnceRealTV.setVisibility(View.GONE);
                            mWithOrWithoutRealTV.setText("实名信息已提交");
                        }
                    });
                } else {
                    showToast(idCardResult.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish_bt:
                if (items.size() == 0) {
                    return;
                }
                if (items.size() == 1) {
                    NewParcelBean parcelBean = items.get(0);
                    if (parcelBean.isRejectStatus()) {
                        requestData();
                        //                        goToMainActivity();
                    } else if (isEmpty(parcelBean.getBagCode()) && parcelBean.getTaskType() != Const.CourierTaskStatus.sameCityTask) {
                        showToast("您有包裹未绑定投递编号，请扫描投递袋背部二维码或条形码完成绑定");
                        return;
                    } else {
                        requestData();
                    }
                } else {
                    boolean bagCodeFag = false;//false 都绑定了袋子编号  true 有一个或多个没有绑定袋子编号
                    boolean rejectFag = false;// true 包裹被拒收了  false 包裹没有被拒收
                    for (NewParcelBean bean : mAdapter.getItems()) {
                        //如果都为拒收的情况下，不需要检测是否绑定了袋子，直接返回首页
                        if (bean.isRejectStatus()) {
                            rejectFag = true;
                        } else {//没有拒收
                            rejectFag = false;
                            break;
                        }
                    }
                    //包裹都为拒收的情况
                    if (rejectFag) {
                        requestData();
                        break;
                        //                        goToMainActivity();
                    } else {
                        //如果有没有拒收的包裹，再进行检测是否有未绑定袋子编号的
                        for (NewParcelBean bean : mAdapter.getItems()) {
                            //如果包裹没有绑定袋子编号,并且包裹是没有拒收 则进行提示
                            if (isEmpty(bean.getBagCode()) && !bean.isRejectStatus() && bean.getTaskType() != Const.CourierTaskStatus.sameCityTask) {
                                bagCodeFag = true;
                                break;
                            } else {
                                bagCodeFag = false;
                            }
                        }
                    }
                    if (bagCodeFag) {
                        showToast("您有包裹未绑定投递编号，请扫描投递袋背部二维码或条形码完成绑定");
                        return;
                    }
                    //如果有没有拒收的包裹并且都绑定了袋子编号，则进行相应的接口处理
                    //否则 则是都拒收的包裹，直接回到首页
                    requestData();
                    //                    if (!rejectFag && !bagCodeFag) {
                    //                        requestData();
                    //                    } else {
                    //                        goToMainActivity();
                    //                    }
                }
                break;
            case R.id.at_once_real_tv:
                if (mAdapter != null && mAdapter.getItemsSize() > 0 && mAdapter.getItem(0) != null) {
                    NewParcelBean parcelBean = mAdapter.getItem(0);
                    DialogMgr.realPopupWindow(getContext(), findViewById(R.id.finish_bt), parcelBean);
                }
                break;
        }
    }

    /**
     * 判断是否直接回到首页
     */
    private boolean isGoToMainActivity() {
        if (items.size() == 1) {
            NewParcelBean parcelBean = items.get(0);
            if (parcelBean.isRejectStatus()) {
                return true;
            }
            return false;
        } else {
            boolean bagCodeFag = false;//false 都绑定了袋子编号  true 有一个或多个没有绑定袋子编号
            boolean rejectFag = false;// true 包裹被拒收了  false 包裹没有被拒收
            for (NewParcelBean bean : mAdapter.getItems()) {
                //如果都为拒收的情况下，不需要检测是否绑定了袋子，直接返回首页
                if (bean.isRejectStatus()) {
                    rejectFag = true;
                } else {//没有拒收
                    rejectFag = false;
                    break;
                }
            }
            //包裹都为拒收的情况
            if (rejectFag) {
                return true;
            } else {
                //如果有没有拒收的包裹，再进行检测是否有未绑定袋子编号的
                for (NewParcelBean bean : mAdapter.getItems()) {
                    //如果包裹没有绑定袋子编号,并且包裹是没有拒收 则进行提示
                    if (isEmpty(bean.getBagCode()) && !bean.isRejectStatus()) {
                        bagCodeFag = true;
                        break;
                    } else {
                        bagCodeFag = false;
                    }
                }
            }
            //如果有没有拒收的包裹并且都绑定了袋子编号，则进行相应的接口处理
            //否则 则是都拒收的包裹，直接回到首页
            if (!rejectFag && !bagCodeFag) {
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * 返回首页并进行刷新
     */
    private void goToMainActivity() {
        showToast("任务取消成功");
        AppLogic.courierBackToHomeAndRefresh(getContext());
        finish();
    }

    /**
     * 请求数据
     */
    private void requestData() {
        showLoadingDialog(null);
        if (Const.CourierTaskStatus.sameCityTask == items.get(0).getTaskType()) {
            AppHttp.getInstance().sameCityParcelComplete(items.get(0).getTaskId());
        } else {
            AppHttp.getInstance().courierFinishParcelTakeData(items.get(0).getTaskId());
        }
    }

    private void initAccessTokenWithAkSk() {
        OCR.getInstance().initAccessTokenWithAkSk(new OnResultListener<AccessToken>() {
            @Override
            public void onResult(AccessToken result) {
                String token = result.getAccessToken();
                hasGotToken = true;
                //  初始化本地质量控制模型,释放代码在onDestory中
                //  调用身份证扫描必须加上 intent.putExtra(CameraActivity.KEY_NATIVE_MANUAL, true); 关闭自动初始化和释放本地模型
                CameraNativeHelper.init(getContext(), OCR.getInstance().getLicense(),
                        new CameraNativeHelper.CameraNativeInitCallback() {
                            @Override
                            public void onError(int errorCode, Throwable e) {
                                String msg;
                                switch (errorCode) {
                                    case CameraView.NATIVE_SOLOAD_FAIL:
                                        msg = "加载so失败，请确保apk中存在ui部分的so";
                                        break;
                                    case CameraView.NATIVE_AUTH_FAIL:
                                        msg = "授权本地质量控制token获取失败";
                                        break;
                                    case CameraView.NATIVE_INIT_FAIL:
                                        msg = "本地质量控制";
                                        break;
                                    default:
                                        msg = String.valueOf(errorCode);
                                }
                                Log.d("11111", "本地质量控制初始化错误，错误原因： " + msg);
                            }
                        });
            }

            @Override
            public void onError(OCRError error) {
                error.printStackTrace();
                showToast("文字识别验证失败");
            }
        }, BeeApplication.getContext(), Constants.OCR_APP_KEY, Constants.OCR_APP_SECRET);
    }

    /**
     * 设置身份证实名认证
     *
     * @param type 0 智能扫描  1 手动录入信息
     */
    public void setIdCardInfo(int type) {
        Intent intent = null;
        if (type == 1) {
            if (mAdapter != null && mAdapter.getItemsSize() > 0 && mAdapter.getItem(0) != null) {
                intent = new Intent(getContext(), SenderAuthenticationActivity.class);
                intent.putExtra("item", mAdapter.getItem(0));
                startActivityForResult(intent, 103);
            } else {
                showToast("数据有误");
            }
        } else {
            if (hasGotToken) {
                intent = new Intent(getContext(), OCRCameraActivity.class);
                intent.putExtra(Const.KEY_OUTPUT_FILE_PATH, FileUtils.getSaveFile(getContext()).getAbsolutePath());
                intent.putExtra(Const.KEY_NATIVE_ENABLE,
                        true);
                // KEY_NATIVE_MANUAL设置了之后CameraActivity中不再自动初始化和释放模型
                // 请手动使用CameraNativeHelper初始化和释放模型
                // 推荐这样做，可以避免一些activity切换导致的不必要的异常
                intent.putExtra(Const.KEY_NATIVE_MANUAL,
                        true);
                intent.putExtra(Const.KEY_CONTENT_TYPE, Const.CONTENT_TYPE_ID_CARD_FRONT);
                startActivityForResult(intent, REQUEST_CODE_CAMERA);
            } else {
                showToast("身份证识别初始化有误");
            }
        }
    }

    /**
     * 扫描身份证
     *
     * @param idCardSide
     * @param filePath
     * @param imagePath
     */
    private void recIDCard(final String idCardSide, final String filePath, final String imagePath) {
        IDCardParams param = new IDCardParams();
        param.setImageFile(new File(filePath));
        // 设置身份证正反面
        param.setIdCardSide(idCardSide);
        // 设置方向检测
        param.setDetectDirection(true);
        // 设置图像参数压缩质量0-100, 越大图像质量越好但是请求时间越长。 不设置则默认值为20
        param.setImageQuality(20);

        OCR.getInstance().recognizeIDCard(param, new OnResultListener<IDCardResult>() {
            @Override
            public void onResult(IDCardResult result) {
                if (result != null) {
                    if (IDCardParams.ID_CARD_SIDE_FRONT.equals(idCardSide)) {
                        if (isNotEmpty(imagePath)) {
                            setIDCardBitmap(result, imagePath);
                        }
                    }
                }
                Log.d("11111", result.toString());
            }

            @Override
            public void onError(OCRError error) {
                Log.e("11111", "文字识别异常", error);
            }
        });
    }

    /**
     * 设置身份证扫描图片
     *
     * @param result
     * @param path
     */
    private void setIDCardBitmap(IDCardResult result, String path) {
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        if (bitmap != null && isNotEmpty(result.getName()) && isNotEmpty(result.getIdNumber())) {
            idCardName = result.getName().toString();
            idCardNumber = result.getIdNumber().toString();
            showLoadingDialog("正在上传");
            AppHttp.getInstance().postFils(MsgID.courier_parcel_list_scan_userIdCard_image, path);
        } else if (isNotEmpty(result.getName()) && isNotEmpty(result.getIdNumber())) {
            showLoadingDialog("正在上传");
            AppHttp.getInstance().courierParcelListUserIdCardSubmit(mAdapter.getItem(0).getOrderUserPhone(), idCardName, idCardNumber, "", "");
        } else {
            showToast("识别失败");
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && 1005 == requestCode) {
            String capturePath = data.getStringExtra("file");
            if (!Str.isEmpty(capturePath)) {
                showLoadingDialog("正在上传图片");
                AppHttp.getInstance().postFils(capturePath);
            }
        }
        // 扫描袋子编号返回
        else if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("result");
            if (StringUtils.isEmpty(result)) {
                return;
            }
            int index = result.lastIndexOf("=");
            String result2;
            if (index > -1) {
                result2 = result.substring(index + 1, result.length());
            } else {
                result2 = result;
            }
            if (parcelBean != null) {
                bagCode = result2;
                showLoadingDialog(null);
                AppHttp.getInstance().bagCodeBindParcel1(result2, parcelBean.getParcelId(), position);
            }
        }
        //在查看大图片返回刷新局部item
        else if (requestCode == 1007 && resultCode == Activity.RESULT_OK) {
            NewParcelBean item = (NewParcelBean) data.getSerializableExtra("item");
            int currentPosition = data.getIntExtra("currentPosition", 0);
            if (item != null) {
                mAdapter.updateItemView(currentPosition, item);
            }
        }
        //包裹拒收
        else if (requestCode == 1008 && resultCode == Activity.RESULT_OK) {
            NewParcelBean item = (NewParcelBean) data.getSerializableExtra("item");
            int currentPosition = data.getIntExtra("currentPosition", 0);
            if (item != null) {
                mAdapter.updateItemView(currentPosition, item);
            }
        }
        //身份证扫描返回
        else if (requestCode == REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                String contentType = data.getStringExtra(Const.KEY_CONTENT_TYPE);
                String filePath = FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath();
                String imagePath = data.getStringExtra("filePath");
                if (!TextUtils.isEmpty(contentType)) {
                    if (Const.CONTENT_TYPE_ID_CARD_FRONT.equals(contentType)) {
                        recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath, imagePath);
                    }
                }
            }
        }
        //手动录入身份证信息返回
        else if (requestCode == 103 && resultCode == Activity.RESULT_OK) {
            mAtOnceRealTV.setVisibility(View.GONE);
            mWithOrWithoutRealTV.setText("实名信息已提交");
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
